AlertDialog 是一種基本的對話方塊,裡面預設可以顯示訊息、顯示列表,並提供了一些基本決策按鈕,那今天就做一個簡單的對話方塊吧。
今天我會把之前做的註冊畫面加入AlertDialog。
package com.example.test1;
import ...
public class Register extends AppCompatActivity {
//宣告物件
private Button cler,comfirm;
private EditText userid,userpasswd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
init();//取得元件的id
buttonlistener();//設定監聽事件
}
//取得元件的id
private void init() {
userid=(EditText)findViewById(R.id.userid);
userpasswd=(EditText)findViewById(R.id.userpasswd);
comfirm=(Button)findViewById(R.id.comfirm);
cler=(Button)findViewById(R.id.cler);
}
//設定監聽事件
private void buttonlistener() {
comfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//新增一個sharedpreference
SharedPreferences sharedPreferences =getSharedPreferences("data",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
//存入資料
editor.putString("id",userid.getText().toString());
editor.putString("password",userpasswd.getText().toString());
editor.apply();
String str =userid.getText().toString();
String str1 =userpasswd.getText().toString();
//判斷輸入的值是否為空值
if (TextUtils.isEmpty(str)) {
Toast.makeText(Register.this, "帳號不能為空",Toast.LENGTH_SHORT).show();
}else if (TextUtils.isEmpty(str1)) {
Toast.makeText(Register.this, "密碼不能為空", Toast.LENGTH_SHORT).show();
}else {
final AlertDialog.Builder alertdialog=new AlertDialog.Builder(Register.this);
alertdialog.setTitle("提醒");
alertdialog.setMessage("確定註冊??");
alertdialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Register.this,"註冊成功!",Toast.LENGTH_SHORT).show();
Intent intent1=new Intent(Register.this,MainActivity.class);
startActivity(intent1);
}
});
alertdialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Register.this,"不行!你只能點確定",Toast.LENGTH_SHORT).show();
}
});
alertdialog.setCancelable(false);
alertdialog.show();
}
}
});
//按下清除按鈕則清除帳號密碼
cler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userid.setText("");
userpasswd.setText("");
}
});
}
}
建立一個AlertDialog,並新增標題跟內容
final AlertDialog.Builder alertdialog=new AlertDialog.Builder(Register.this);
alertdialog.setTitle("提醒");
alertdialog.setMessage("確定註冊??");
設定按鈕監聽
//設定右邊的按鈕
alertdialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Register.this,"註冊成功!",Toast.LENGTH_SHORT).show();
Intent intent1=new Intent(Register.this,MainActivity.class);
startActivity(intent1);
}
});
設定左邊的按鈕
alertdialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Register.this,"不行!你只能點確定",Toast.LENGTH_SHORT).show();
}
});
setCancelable()方法可以設置是否接受返回鍵使用
alertdialog.setCancelable(false);
將製作好的AlertDialog顯示
alertdialog.show();
成果圖